package com.ant.cn.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.ant.cn.R;

/**
 * Created by baichuan on 2016/12/6.
 */

public class MyTextView extends View {
    private int borderColor=0;
    private float borderHeight=0;

    private RectF rect = new RectF();
    private Paint paint = new Paint();

    public MyTextView(Context context) {
        super(context);
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initThisView(context, attrs, defStyleAttr);
    }

    private void initThisView(Context context, AttributeSet attrs, int defStyleAttr) {

        TypedArray tp = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);
        borderColor = tp.getColor(R.styleable.MyTextView_antBorderColor, 0);
        borderHeight = tp.getFloat(R.styleable.MyTextView_antBotderHeight, 0);
        tp.recycle();

        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(borderColor);
        paint.setStrokeWidth(borderHeight);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(measuerSize(widthMeasureSpec), measuerSize(heightMeasureSpec));
    }

   private int measuerSize(int measuerspec){
       int result=30;
       int model=MeasureSpec.getMode(measuerspec);
       int size=MeasureSpec.getSize(measuerspec);
       if(MeasureSpec.EXACTLY==model){
           result=size;
       }else{
           result=size;
           if(model==MeasureSpec.AT_MOST)
               result=Math.min(size,result);
       }
       return result;
   }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        rect.set(0, 0, w, h);
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        canvas.drawRect(rect, paint);
        super.onDraw(canvas);
    }
}
